import Vue from 'vue'
import Router from 'vue-router'

//网站前台
import MainView from '@/views/blog/MainView';
import Index from "@/views/blog/index/Index";// 首页
import Technology from "@/views/blog/technology/Technology";//技术专区
import ArticleList from "@/views/blog/Article/ArticleList";//文章分类列表
import ArticleInfo from "@/views/blog/Article/ArticleInfo";//文章内容

//后台管理
import Login from "@/views/admin/Login";//登录
import Admin from "@/views/admin/Admin";//后台框架
import AdminIndex from "@/views/admin/index/Index";//主页
import MainArticle from "@/views/admin/article/MainArticle";//文章管理
import ArtList from "@/views/admin/article/ArticleList";//文章列表
import AddList from "@/views/admin/article/AddArticle";//添加文章
import DraftList from "@/views/admin/article/DraftList";//草稿箱
import BackList from "@/views/admin/article/BackList";//回收站
import MainBase from "@/views/admin/base/MainBase";//基础管理
import TypeList from "@/views/admin/base/TypeList";//分类管控
import UserControl from "@/views/admin/users/UserControl";//用户管理
import SwiperList from "@/views/admin/base/SwiperList";//幻灯片修改
import UrlList from "@/views/admin/base/UrlList";//友链

import store from "@/store/Store";//引入仓库

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      name: 'Main',
      component: MainView,
      children: [
        { path: '/', name: 'Index', component: Index },
        { path: '/technology', name: 'Technology', component: Technology },
        { path: '/list', name: 'List', component: ArticleList },
        { path: '/info', name: 'Info', component: ArticleInfo },
      ]
    },
    // 后台管理入口路由
    {
      path: '/admin',
      name: 'Admin',
      component: Admin,
      redirect: '/admain',
      meta: {
        loginRequire: true
      },
      children: [
        {
          path: '/admain', name: 'Admain', component: AdminIndex, meta: { loginRequire: true }
        },
        {
          path: '/mainArticle',
          name: 'MainArticle',
          component: MainArticle,
          meta: { loginRequire: true },
          redirect: '/artList',
          children: [
            { path: '/addList', name: 'AddList', component: AddList },
            { path: '/artList', name: 'ArticleList', component: ArtList },
            { path: '/draftList', name: 'DraftList', component: DraftList },
            { path: '/backList', name: 'BackList', component: BackList },
          ]
        },
        {
          path: '/mainBase',
          name: 'MainBase',
          component: MainBase,
          meta: { loginRequire: true },
          redirect: '/swiperList',
          children: [
            { path: '/typeList', name: 'TypeList', component: TypeList },
            { path: '/swiperList', name: 'SwiperList', component: SwiperList },
            { path: '/urls', name: 'UrlList', component: UrlList },
            { path: '/backList', name: 'BackList', component: BackList }
          ]
        },
        {
          path: '/users',
          name: 'USerControl',
          component: UserControl,
          meta: { loginRequire: true },
          children: [
            { path: '/addList', name: 'AddList', component: AddList },
            { path: '/artList', name: 'ArticleList', component: ArtList },
            { path: '/draftList', name: 'DraftList', component: DraftList },
            { path: '/backList', name: 'BackList', component: BackList }
          ]
        }
      ]
    },
    { path: '/login', name: 'Login', component: Login },
    { path: '*', redirect: '/' }
  ]
});

// 全局路由守卫
router.beforeEach((to, from, next) => {
  // 遍历路由
  let flag = to.matched.some(function (item) {
    return item.meta.loginRequire
  });

  // 判断是否要登录
  const isLogin = store.getters.isLogins;//是否登录
  if (flag) {
    if (isLogin) { next(); } else next('/login');
  } else
    //已经登录直接跳转首页
    if (to.name === 'Login') {
      if (isLogin) { next('/admin'); } else next();
    } else
      next();
});

export default router;
